<?php if(!defined('DB_NAME')) die();

//添加编辑器按钮样式脚本
if ( is_admin() ){
	wp_enqueue_style('thickbox');
	wp_enqueue_style('wp-color-picker');
	wp_enqueue_style('my-hack', get_template_directory_uri().'/res/css/hack.css');
	wp_enqueue_script('thickbox');
	wp_enqueue_script('custom-script-handle');
	wp_enqueue_script('my-script-handle', get_template_directory_uri().'/res/js/editor.js', array( 'wp-color-picker' ), false, true );
}

//编辑器短代码
//音乐，支持虾米&网易云音乐
function cleris_music($atts, $music = null){
	extract( shortcode_atts(array('autoplay' => null, 'loop' => null), $atts) );
	if(preg_match('#http://www.xiami.com/song/([^\/]+)(\?*|)#i', $music, $matches)) {
		global $INIJSON;
		$song_id = $matches[1];
		$xiami_token = $INIJSON->song_list($song_id);
		$music = $xiami_token['songs'][0]['song_src'];
	}
	if(preg_match('#http://music.163.com/\#/song\?id=([^\/]+)#i', $music, $matches)) {
		global $INIJSON;
		$song_id = $matches[1];
		$netease_token = $INIJSON->netease_song($song_id);
		$music = $netease_token['song_src'];
	}
	//添加音乐后缀，避免播放出错
	$_str = explode('.', $music);
	$music_format = strtolower($_str[count($_str)-1]);
	$format = array('mp3', 'wav', 'ogg', 'webm');
	if( !in_array($music_format, $format) ) $music = ereg('\?', $music) ? $music . "&.mp3" : $music . "?.mp3";
	//提交audio标签，并添加flash兼容IE8
	$autostart = $autoplay != null ? 'yes' : 'no';
	$_loop = $loop != null ? 'yes' : 'no';
	return '<audio src="'.$music.'" '.$autoplay.' '.$loop.' controls preload="metadata"><embed src='.TPDIR.'/res/mplayer.swf quality="high" width="320" height="30" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="text=0x0088DD&loader=0x8EC2F4&autostart='.$autostart.'&loop='.$_loop.'&soundFile='.$music.'"></audio>';
}
//视频，支持优酷、土豆、腾讯视频
function cleris_video($atts, $video = null){
	extract( shortcode_atts(array('width' => null, 'height' => null), $atts) );
	$datawidth = $width != null? "data-width=$width": '';
	$dataheight = $height != null? "data-height=$height": '';
	if(preg_match('#http://v.youku.com/v_show/id_(.*?).html#i', $video, $matches)){
		return "<div class='click2play slower' $datawidth $dataheight data-value='http://player.youku.com/embed/".esc_attr($matches[1])."'></div>";
	}
	else if(preg_match('#http://www.tudou.com/programs/view/(.*?)/#i', $video, $matches)) {
		return "<div class='click2play slower' $datawidth $dataheight data-value='http://www.tudou.com/programs/view/html5embed.action?code=".esc_attr($matches[1])."'></div>";
	}
	else if(preg_match('/qqtv#([^\/]+)/i', $video, $matches)) {
		return "<div class='click2play slower' $datawidth $dataheight data-value='http://v.qq.com/iframe/player.html?vid={$matches[1]}'></div>";
	} else {
		$vid = explode('|', $video);
		$result = null;
		for( $i=0; $i<count($vid); $i++){
			if( $result === null ) {
				$result = '';
				$abc = '<source src="'.$vid[$i].'">';
			} else {
				$result = '';
				$abc.= '<source src="'.$vid[$i].'">';
			}
		}
		return "<video controls width=$width height=$height>{$abc}<i style=color:red>抱歉！您的浏览器不支持video标签！</i></video>";
	}
}
//下载
function cleris_download($atts, $download = null){
	extract( shortcode_atts(array('title' => null, 'href' => null), $atts) );
	return '<span class="download"><a target="_blank" title="'.$title.'" href="'.$href.'">'.$download.'</a></span>';
}
//外链
function cleris_url($atts, $url = null){
	extract( shortcode_atts(array('title' => null, 'href' => null), $atts) );
	return '<span class="u-download"><a target="_blank" title="'.$title.'" href="'.$href.'" rel="external nofollow" target="_blank">'.$url.'</a></span>';
}
//加密
function private_content($atts, $private = null){
	if ( current_user_can('create_users') ) return '<div class="private">'.$private.'</div>';
	return '悄悄话, 乃们别偷看';
}
//下拉列表
function cleris_list($atts, $text = null){
	extract( shortcode_atts(array('title' => null), $atts) );
	return '<div class="car-list"><button>'.$title.'</button><ul><li>'.$text.'</li></ul></div>';
}
add_shortcode('music', 'cleris_music');
add_shortcode('video', 'cleris_video');
add_shortcode('download', 'cleris_download');
add_shortcode('url', 'cleris_url');
add_shortcode('private', 'private_content');
add_shortcode('list', 'cleris_list');
//评论内容添加短代码
add_filter('comment_text', 'do_shortcode');

//Prettify
if ( get('ini_prettify') ){
	//转义HTML实体
	function strip_autoformat($text){
		$no_p = preg_replace("/<p>/is","",$text);
		$no_br = preg_replace("/<br.?\/>/is","",$no_p);
		$no_lt = preg_replace("/</is","&lt;",$no_br);
		$no_gt = preg_replace("/>/is","&gt;",$no_lt);
		$no_pi = preg_replace("/&lt;\/p&gt;/is","",$no_gt);
		return preg_replace("/<\/p>/is","\n",$no_pi);
	}
	//注册短代码
	function codetag_handler($atts, $content){
		extract( shortcode_atts(
			array(
			'lang' => 'default-markup',
			'quote' => 'true',
			'start' => null,
			'highlight' => null
			),
			$atts )
		);
		return "<pre class='".($quote == 'true' ? "" : "dontquote ").
		"prettyprint lang-$lang".
		($start != null ? " linenums:$start linenumstrigger" : "").
		($highlight != null ? " highlight:$highlight" : "").
		"'>".strip_autoformat($content)."</pre>";
	}
	add_shortcode('code', 'codetag_handler');
	function prettify_gc_clean_code($text){
		return preg_replace( "/<pre\\s*class\\s*=\\s*([\"'])\\s*prettyprint\\s*([^>]*?)\\s*>(.*?)<\/pre>/ise", "'<pre class='.stripslashes('$1').'prettyprint '.stripslashes('$2').'>'.htmlentities( stripslashes('$3') ).'</pre>'", $text );
	}
	add_filter( 'the_content', 'prettify_gc_clean_code', 1 );
	//起始行号
	function prettify_gc_style_trigger($text){
		return preg_replace( "/<pre\\s*class\\s*=([^>]*?)(\\blinenums\\b)([^>]*?)\\s*>/ise", "'<pre class='.stripslashes('$1').stripslashes('$2').'trigger '.stripslashes('$2').stripslashes('$3').'>'", $text );
	}
	add_filter('the_content', 'prettify_gc_style_trigger', 2 );
}
